from urllib.parse import urlencode
from tornado import httpclient
import tornado
from tornado.httpclient import HTTPRequest
from functools import partial
import json
class AsyncYunPian:
    async def send_single_sms(self, code, mobile):
        http_client = httpclient.AsyncHTTPClient()
        url = "http://sms.yunpian.com/v2/sms/single_send.json"
        api_key = "f862c51c7543d3995678ac784574636a"
        data = urlencode({
            "apikey": api_key,
            "mobile": mobile,
            "text": "【云片网】您的验证码是{}".format(code)
        })
        post_request = HTTPRequest(url=url,method="POST",body=data)
        response = await http_client.fetch(post_request)
        return json.loads(response.body.decode('utf8'))

if __name__ == '__main__':
    io_loop = tornado.ioloop.IOLoop.current()
    yun = AsyncYunPian()
    send = partial(yun.send_single_sms,"6666","17607560047")
    io_loop.run_sync(send)
